/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.web.firefox.objectsource;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;

import org.kommando.application.core.Application;
import org.kommando.core.catalog.AbstractObjectSource;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.core.catalog.ObjectSource;
import org.kommando.core.catalog.SimpleCatalogObject;
import org.kommando.core.util.Assert;
import org.kommando.web.WebPage;
import org.kommando.web.firefox.Firefox;

/**
 * {@link ObjectSource} for the firefox plugin.
 * 
 * @author Peter De Bruycker
 */
public class FirefoxObjectSource extends AbstractObjectSource {

    private static final String ID = "web:firefox";

    private static final String BOOKMARK_ID_PREFIX = "web:firefox:bookmark:";

    private Firefox firefox;

    public FirefoxObjectSource(Firefox firefox) {
        Assert.argumentNotNull("firefox", firefox);
        this.firefox = firefox;
    }

    @Override
    public Class<?>[] getExtensionTypes(CatalogObject object) {
        if (object.hasExtension(Application.class) && object.getExtension(Application.class).getName().equals("Firefox")) {
            return new Class<?>[] { Firefox.class };
        }

        return null;
    }

    @Override
    public <T> T getExtension(CatalogObject object, Class<T> extensionType) {
        if (extensionType.equals(Firefox.class)) {
            return extensionType.cast(firefox);
        }

        return null;
    }

    @Override
    public List<CatalogObject> getObjects() {
        List<CatalogObject> objects = new ArrayList<CatalogObject>();

        // objects.addAll(firefox.getBookmarks());

        return objects;
    }

    @Override
    public CatalogObject getObject(String id) {
        if (id.startsWith(BOOKMARK_ID_PREFIX)) {
            String url = id.substring(BOOKMARK_ID_PREFIX.length());
            for (WebPage bookmark : firefox.getBookmarks()) {
                if (url.equals(bookmark.getUrl())) {
                    return createWebPageCatalogObject(bookmark);
                }
            }
        }

        return null;
    }

    @Override
    public List<CatalogObject> getChildren(CatalogObject object) {
        // TODO caching?

        if (object.hasExtension(Firefox.class)) {
            List<CatalogObject> bookmarkObjects = new ArrayList<CatalogObject>();
            List<WebPage> bookmarks = object.getExtension(Firefox.class).getBookmarks();
            for (WebPage bookmark : bookmarks) {
                bookmarkObjects.add(createWebPageCatalogObject(bookmark));
            }

            return bookmarkObjects;
        }

        return null;
    }

    private CatalogObject createWebPageCatalogObject(WebPage webPage) {
        SimpleCatalogObject webPageObject = new SimpleCatalogObject(BOOKMARK_ID_PREFIX + webPage.getUrl(), webPage.getTitle(), webPage.getUrl(),
                webPage.getIcon());
        webPageObject.setExtension(WebPage.class, webPage);

        return webPageObject;
    }

    @Override
    public boolean hasChildren(CatalogObject object) {
        return object.hasExtension(Firefox.class);
    }

    @Override
    public String getId() {
        return ID;
    }

    @Override
    public String getDescription() {
        // TODO description
        return "TODO";
    }

    @Override
    public Icon getIcon() {
        // TODO null
        return null;
    }

    @Override
    public String getName() {
        return "Firefox";
    }
}
